package com.jstyle.jclifexd.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.fragment.RankDayFragment;
import com.jstyle.jclifexd.fragment.RankMonthFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class RankActivity extends BaseActivity {

    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rb_sleep_day)
    RadioButton rbSleepDay;
    @BindView(R.id.rb_sleep_week)
    RadioButton rbSleepWeek;

    @BindView(R.id.rg_history)
    RadioGroup rgHistory;
    @BindView(R.id.FrameLayout_rank)
    FrameLayout FrameLayoutRank;


    private List<Fragment> fragmentList = new ArrayList<>();
    private FragmentManager fragmentManager;
    private final int FRAGMENT_Day = 0;
    private final int FRAGMENT_Month = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank);
        ButterKnife.bind(this);
        fragmentManager = getSupportFragmentManager();
        if (savedInstanceState != null) {
            fragmentList = fragmentManager.getFragments();
        }
        init();
    }

    private static final String TAG = "RankActivity";

    private void init() {


        rgHistory.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_sleep_day:
                        startTransaction(FRAGMENT_Day);
                        break;
                    case R.id.rb_sleep_week:
                        startTransaction(FRAGMENT_Month);
                        break;
                }
            }
        });
        startTransaction(FRAGMENT_Day);
    }

    protected void startTransaction(int fragmentId) {
        //  Log.i(TAG, "startTransaction: "+fragmentId);
        if (fragmentId == -1) return;
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        Fragment fragment = findFragment(fragmentId);
        if (fragment != null) {
            fragmentTransaction.show(findFragment(fragmentId));
        } else {
            switch (fragmentId) {
                case FRAGMENT_Month:
                    fragment = new RankMonthFragment();
                    break;
                case FRAGMENT_Day:
                    fragment = new RankDayFragment();
                    break;
            }
            addFragmentTag(fragmentTransaction, fragmentId, fragment);
        }

        fragmentTransaction.commitAllowingStateLoss();
    }

    private void addFragmentTag(FragmentTransaction fragmentTransaction, int tag, Fragment fragment) {
        fragmentList.add(fragment);
        fragmentTransaction.add(R.id.FrameLayout_rank, fragment, String.valueOf(tag));
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        for (Fragment fragment : fragmentList) {
            fragmentTransaction.hide(fragment);
        }
    }

    private Fragment findFragment(int tag) {
        for (Fragment fragment : fragmentList) {
            if (fragment.getTag().equals(String.valueOf(tag))) return fragment;
        }
        return null;
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
